package searchandsort;

import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

public class MinimumTimeDifference539 {
    //todo 精简代码
    int halfDay = 12 * 60;
    int oneDay = 24 * 60;

    public int findMinDifference(List<String> timePoints) {
        if (timePoints.size() < 2) {
            return 0;
        }
        TreeSet<String> set = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return bigger(o1, o2)?1:-1;
            }
        });
        int result = getSub(timePoints.get(0), timePoints.get(1));
        set.add(timePoints.get(0));
        set.add(timePoints.get(1));
        for (int i = 2; i < timePoints.size(); i++) {
            String floor = set.floor(timePoints.get(i));
            if (floor == null) {
                floor = set.last();
            }
            int sub = getSub(timePoints.get(i), floor);
            result = Math.min(sub, result);
            String ceiling = set.ceiling(timePoints.get(i));
            if (ceiling == null) {
                ceiling = set.first();
            }
             sub = getSub(timePoints.get(i), ceiling);
            result = Math.min(sub, result);
            set.add(timePoints.get(i));
        }
        return result;
    }

    int getSub(String time1, String time2) {
        if (!bigger(time1, time2)) {
            String temp = time1;
            time1 = time2;
            time2 = temp;
        }
        String[] time1s = time1.split(":");
        String[] time2s = time2.split(":");
        int result = 0;
        result += (Integer.valueOf(time1s[0]) - Integer.valueOf(time2s[0])) * 60;
        result += (Integer.valueOf(time1s[1]) - Integer.valueOf(time2s[1]));
        if(result>halfDay){
            result = oneDay-result;
        }
        return result;
    }

    boolean bigger(String time1, String time2) {
        String[] time1s = time1.split(":");
        String[] time2s = time2.split(":");
        if (Integer.valueOf(time1s[0]) > Integer.valueOf(time2s[0])) {
            return true;
        }
        if (Integer.valueOf(time1s[0]) == Integer.valueOf(time2s[0]) &&
                Integer.valueOf(time1s[1]) > Integer.valueOf(time2s[1])) {
            return true;
        }
        return false;
    }

//    public int findMinDifference(List<String> timePoints) {
//        TreeSet<String> treeSet = new TreeSet(this::compare);
//        int result = Integer.MAX_VALUE;
//        for (int i = 0; i < timePoints.size(); i++) {
//            String s = timePoints.get(i);
//            if (treeSet.contains(s)) {
//                return 0;
//            }
//            String lower = treeSet.lower(s);
//            String higher = treeSet.higher(s);
//            if(lower!=null){
//                int sub = sub(s, lower);
//                if (sub < result) {
//                    result = sub;
//                }
//            }
//            if(higher!=null){
//                int sun = sub(higher, s);
//                result = result > sun ? sun : result;
//            }
//            treeSet.add(s);
//        }
//        return result;
//    }
//
//    private int sub(String s1, String s2) {
//        String[] ss1 = s1.split(":");
//        String[] ss2 = s2.split(":");
//        int result = 0;
//        int res = 0;
//        int minutes = Integer.valueOf(ss1[1]) - Integer.valueOf(ss2[1]);
//        if (minutes < 0) {
//            minutes += 60;
//            res = -1;
//        }
//        result += minutes;
//        result += (Integer.valueOf(ss1[0]) - Integer.valueOf(ss2[0]) + res) * 60;
//
//        return result;
//    }
//
//
//    private int compare(Object s1, Object s2) {
//        String[] ss1 = ((String) s1).split(":");
//        String[] ss2 = ((String) s2).split(":");
//        if (ss1[0] == ss2[0]) {
//            return Integer.valueOf(ss1[1]) - Integer.valueOf(ss2[1]);
//        } else {
//            return Integer.valueOf(ss1[0]) - Integer.valueOf(ss2[0]);
//        }
//    }
}
